# Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
# Copyright (C) 2006-2010 RobotCub Consortium
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

# include CMake Java utilities and JNI
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include(UseJava)

# this is set in the parent directory, but for some reason it does not get inherited
set_source_files_properties(${SWIG_BINDINGS_SOURCE_FILE} PROPERTIES CPLUSPLUS ON)

if (${Java_VERSION_MAJOR} VERSION_GREATER_EQUAL 9)
  # Java >= 9
  set(JAVA_RELEASE_VERSION 7 CACHE STRING "Compile for a specific VM version. See javac --help (--release option)")
  mark_as_advanced(JAVA_RELEASE_VERSION)
  if(NOT JAVA_RELEASE_VERSION STREQUAL "")
    list(APPEND CMAKE_JAVA_COMPILE_FLAGS "--release" "${JAVA_RELEASE_VERSION}")
  endif()
  # clean old variable
  unset(JAVA_TARGET_VERSION CACHE)
  unset(JAVA_SOURCE_VERSION CACHE)
else()
  # Java < 9: --release option not present
  set(JAVA_TARGET_VERSION ${Java_VERSION_MINOR} CACHE STRING "Java target version. See javac --help (-target option)")
  set(JAVA_SOURCE_VERSION ${Java_VERSION_MINOR} CACHE STRING "Java source version. See javac --help (-source option)")
  mark_as_advanced(JAVA_TARGET_VERSION)
  mark_as_advanced(JAVA_SOURCE_VERSION)
  if(NOT JAVA_TARGET_VERSION STREQUAL "")
    list(APPEND CMAKE_JAVA_COMPILE_FLAGS "-target" "${JAVA_TARGET_VERSION}")
  endif()
  if(NOT JAVA_SOURCE_VERSION STREQUAL "")
    list(APPEND CMAKE_JAVA_COMPILE_FLAGS "-source" "${JAVA_SOURCE_VERSION}")
  endif()
  unset(JAVA_RELEASE_VERSION CACHE)
endif()

set(JAVA_FLAGS "" CACHE STRING "Additional flags to be passed to javac. E.g. -bootclasspath if you want to cross compile")
mark_as_advanced(JAVA_FLAGS)
list(APPEND CMAKE_JAVA_COMPILE_FLAGS "${JAVA_FLAGS}")

# SWIG JAVA file generation
set(TARGET_DIR generated_src/java)

set(CMAKE_SWIG_OUTDIR "${YARP_BASE_DIR}/${TARGET_DIR}")
set(CMAKE_SWIG_FLAGS "-package;yarp;-module;yarp;${SWIG_COMMON_FLAGS}")

file(MAKE_DIRECTORY "${YARP_BASE_DIR}/${TARGET_DIR}")

set(JAVA_MATLAB_UTILS_SRCS src/LoadYarp.java
                           src/YarpImageHelper.java)

# this includes are needed to compile the JNI lib
include_directories(SYSTEM ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})

swig_add_library(yarp_java
                 LANGUAGE java
                 SOURCES ${SWIG_BINDINGS_SOURCE_FILE})

swig_link_libraries(yarp_java ${SWIG_YARP_LIBRARIES})

# issue on MINGW where C++ name-mangling prevents java finding methods
if(MINGW)
  message(STATUS "untested MINGW patch - see CMakeLists.txt")
  set_target_properties(yarp_java PROPERTIES
    LINK_FLAGS "--add-stdcall-alias")
endif(MINGW)

set(_CMAKE_INSTALL_JNIDIR "${CMAKE_INSTALL_LIBDIR}/jni")
set(CMAKE_INSTALL_JNIDIR ${_CMAKE_INSTALL_JNIDIR} CACHE PATH "java bindings (${_CMAKE_INSTALL_JNIDIR})")
mark_as_advanced(CMAKE_INSTALL_JNIDIR)
if(NOT IS_ABSOLUTE ${CMAKE_INSTALL_JNIDIR})
  set(CMAKE_INSTALL_FULL_JNIDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_JNIDIR}")
else()
  set(CMAKE_INSTALL_FULL_JNIDIR "${CMAKE_INSTALL_JNIDIR}")
endif()

set_target_properties(${SWIG_MODULE_yarp_java_REAL_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_JNIDIR})

# Update RPATH
if(NOT CMAKE_SKIP_RPATH AND NOT CMAKE_SKIP_INSTALL_RPATH)
  file(RELATIVE_PATH _rel_path "${CMAKE_INSTALL_FULL_JNIDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}")
  get_target_property(_current_rpath "${SWIG_MODULE_yarp_java_REAL_NAME}" INSTALL_RPATH)
  if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
    list(APPEND _current_rpath "@loader_path/${_rel_path}")
  else()
    list(APPEND _current_rpath "\$ORIGIN/${_rel_path}")
  endif()
  set_target_properties(${SWIG_MODULE_yarp_java_REAL_NAME} PROPERTIES INSTALL_RPATH "${_current_rpath}")
endif()


set(CMAKE_JNI_TARGET TRUE)
set(CMAKE_JAVA_TARGET_OUTPUT_DIR "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_DATADIR}/yarp/java")

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/GenerateSwigJavaFileList.cmake"
"file(GLOB _JAVA_COMPILE_FILES \"${CMAKE_SWIG_OUTDIR}/*.java\")
string(REPLACE \";\" \"\\\"\\n\\\"\" _JAVA_COMPILE_STRING \"\\\"\${_JAVA_COMPILE_FILES}\\\"\")
file(MAKE_DIRECTORY \"${CMAKE_JAVA_TARGET_OUTPUT_DIR}\")
file(WRITE \"${CMAKE_JAVA_TARGET_OUTPUT_DIR}${CMAKE_FILES_DIRECTORY}/yarp_java_jar.dir/swig_java_filelist\" \${_JAVA_COMPILE_STRING})
")

add_custom_command(TARGET ${SWIG_MODULE_yarp_java_REAL_NAME} POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/GenerateSwigJavaFileList.cmake"
                   BYPRODUCTS "${CMAKE_JAVA_TARGET_OUTPUT_DIR}${CMAKE_FILES_DIRECTORY}/yarp_java_jar.dir/swig_java_filelist"
                   COMMENT "Generating list of java files generated by swig")


add_jar(yarp_java_jar
        SOURCES @${CMAKE_JAVA_TARGET_OUTPUT_DIR}${CMAKE_FILES_DIRECTORY}/yarp_java_jar.dir/swig_java_filelist
        OUTPUT_NAME yarp
        VERSION ${YARP_VERSION})

# Matlab Java Utilities
add_jar(yarp_java_matlab_jar
        SOURCES ${JAVA_MATLAB_UTILS_SRCS}
        INCLUDE_JARS yarp_java_jar
        OUTPUT_NAME yarp_matlab_java
        VERSION ${YARP_VERSION})

add_dependencies(yarp_java_jar ${SWIG_MODULE_yarp_java_REAL_NAME})

# installation part
install(TARGETS ${SWIG_MODULE_yarp_java_REAL_NAME}
        DESTINATION ${CMAKE_INSTALL_JNIDIR}
        COMPONENT bindings_java)

install_jar(yarp_java_jar
            DESTINATION ${CMAKE_INSTALL_DATADIR}/yarp/java
            COMPONENT bindings_java)
install_jni_symlink(yarp_java_jar
                    DESTINATION ${CMAKE_INSTALL_DATADIR}/java
                    COMPONENT bindings_java)
install_jar(yarp_java_matlab_jar
            DESTINATION ${CMAKE_INSTALL_DATADIR}/yarp/java
            COMPONENT bindings_java)
install_jni_symlink(yarp_java_matlab_jar
                    DESTINATION ${CMAKE_INSTALL_DATADIR}/java
                    COMPONENT bindings_java)

if(YARP_COMPILE_TESTS)
  # add_subdirectory(tests)
endif()
